

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;


/**
 * Servlet implementation class EditHostFamily
 */
@WebServlet("/EditHostFamily")
public class EditHostFamily extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int hostUserID;
	private int districtID;
	private double publicTransportDistance;
	private int minStayDay;
	private int maxStayDay;
	private int numberOfAdult;
	private int numberOfChildren;
	private String isSmokingAllowed;
	private String isMealProvided;
	private double pricePerMeal;
	private String isPetHeld;
	private String isInternetProvided;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditHostFamily() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession hostsession = request.getSession(true);
		hostsession.getAttribute("hostUserID");
		hostUserID = Integer.parseInt(hostsession.getAttribute("hostUserID").toString());
		System.out.println("hostUserID: " + hostUserID);
		districtID = Integer.parseInt(request.getParameter("district"));
		publicTransportDistance = Double.parseDouble(request.getParameter("publicTransportDistance").toString()); 
		minStayDay = Integer.parseInt(request.getParameter("minStayDay"));
		maxStayDay = Integer.parseInt(request.getParameter("maxStayDay"));
		numberOfAdult = Integer.parseInt(request.getParameter("numberOfAdult"));
		numberOfChildren = Integer.parseInt(request.getParameter("numberOfChildren"));
		isSmokingAllowed = request.getParameter("isSmokingAllowed");
		isMealProvided = request.getParameter("isMealProvided");
		pricePerMeal = Double.parseDouble(request.getParameter("pricePerMeal"));
		isPetHeld = request.getParameter("isPetHeld");
		isInternetProvided = request.getParameter("isInternetProvided");
		
		int hostFamilyID = Integer.parseInt(hostsession.getAttribute("hostFamilyID").toString());
		System.out.println(hostFamilyID);
		hostFamily hostFamilyObj = new hostFamily();
		if(hostFamilyID>0){
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		        session.beginTransaction();
		        hostFamilyObj = (hostFamily) session.get(hostFamily.class,hostFamilyID);
		        session.getTransaction().commit();
			} catch(Exception ex){
				HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			}
		}
		hostFamilyObj.setHostUserID(hostUserID);
		hostFamilyObj.setDistrictID(districtID);
		hostFamilyObj.setPublicTransportDistance(publicTransportDistance);
		hostFamilyObj.setMinStayDay(minStayDay);
		hostFamilyObj.setMaxStayDay(maxStayDay);
		hostFamilyObj.setNumberOfAdult(numberOfAdult);
		hostFamilyObj.setNumberOfChildren(numberOfChildren);
		hostFamilyObj.setIsSmokingAllowed(isSmokingAllowed);
		hostFamilyObj.setIsMealProvided(isMealProvided);
		hostFamilyObj.setPricePerMeal(pricePerMeal);
		hostFamilyObj.setIsPetHeld(isPetHeld);
		hostFamilyObj.setIsInternetProvided(isInternetProvided);
		System.out.println("isInternetProvided: " + isInternetProvided);
		try{
			System.out.println("isPetHeld: " + isPetHeld);
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	        session.beginTransaction();
	        System.out.println("isPetHeld2: " + isPetHeld);
	        session.saveOrUpdate(hostFamilyObj);
			System.out.println("isMealProvided: " + isMealProvided);
	        session.getTransaction().commit();
		} catch(Exception ex){
			System.out.println("pricePerMeal: " + pricePerMeal);
			HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
		}
	}

}
